<?php
/**
 * 
 * 
 */	
class Module_RopController extends  Zend_Controller_Action {
	public function init() {
        $app = Zend_Registry::get('App');
        $this->view->baseUrl = $baseUrl = $this->_request->getBaseUrl() . "/public/";
        $result = array(
                    array($baseUrl.'lib-js/system/jqwidgets/jqxgrid.grouping.js','js')
					,array($baseUrl.'lib-js/module/Module_Rop.js','js')
                    ,array($baseUrl.'css/themes/blitzer/jquery-ui-1.8.14.custom.css','css')
                    ,array($baseUrl.'css/themes/base/calendar.css','css')                    
                    ,array($baseUrl.'lib-js/ui/jquery.ui.datepicker-vi.js','js')
                    // ,array($baseUrl.'lib-js/system/upload/css/bootstrap.css','css')
                    ,array($baseUrl.'css/module/received.css','css')
                );
        if ($this->_request->isXmlHttpRequest()){
            Zend_Layout::startMvc(array(
                'layoutPath' => $app->layout,
                'layout' => 'partial'
            ));
            $this->view->arrJsCss = Zend_Json::encode($result);
        }else{
            //Cau hinh cho Zend_layout
            Zend_Layout::startMvc(array(
                'layoutPath' => $app->layout,
                'layout' => 'index'
            ));
            $response = $this->getResponse();
            $this->view->currentModulCode = $this->_request->getParam('controller');
            $this->view->currentModulCodeForLeft = $this->_request->getParam('action');
            $this->view->postHeader = Clound_String::registerFile($result);
            //Hien thi file template
            $response->insert('menu', $this->view->renderLayout('menu.phtml', './application/layout/scripts/'));
            $response->insert('submenu', $this->view->renderLayout('submenu.phtml', './application/layout/scripts/'));
            $response->insert('footer', $this->view->renderLayout('footer.phtml', './application/layout/scripts/'));
        }
    }

    // Xu ly thong tin de day ra file index.phtml
    public function indexAction() {
    }

    public function dataAction(){
        $model = new module_models_Record();
        $parames = $this->_request->getParams();
        $parames['Status'] = '';
        $data = $model->printRopGetAll($parames);
        echo Zend_Json::encode($data);
        die;
    }

    /**
     * @see : Thuc hien them/sua van ban den
     *
     */
    public function addAction() {
        if (!$this->_request->isXmlHttpRequest()) {
            return $this->_helper->redirector('index');
        }
        $iRecordID = $this->_request->getParam('RecordID','');
        //Gan gia tri mac dinh cho cac Input
        $model = new module_models_Record();
        $model = $model->selectRow($iRecordID);
        if($iRecordID){
            $recordtype = $model['RecordType'];
            $form = $model['Form'];
        }else{
            $recordtype = $this->_request->getParam('recordtype');
            $form = $this->_request->getParam('form');
            $model['RecordType'] = $recordtype;
            $model['Form'] = $form;
        }
        $this->view->addHelperPath('Clound/Xml/Helper/', 'Clound_Xml_Helper');
        // File xml mô tả form
        $filexml = 'xml/module/receive/'.$form.'.xml';
        $xmlObj = new Clound_Xml($filexml,$model);
        $formObj = $xmlObj->formfieldGenerate();
        $this->view->form = $formObj;
    }
	/**
     * @see : Thuc hien luu van ban den
     *
     */
    public function saveAction() {
        $model = new module_models_Record();
        $iRecordID = $this->_request->getParam('RecordID',null);
        $currentStaff = Zend_Auth::getInstance()->getIdentity();
        
        $data = array(
            "RecordID"      => $iRecordID,
            "RecordType"    => $this->_request->getParam('RecordType'),
            "Form"          => $this->_request->getParam('Form'),
            "RegisterName"  => $this->_request->getParam('RegisterName'),
            "Province"      => $this->_request->getParam('Province'),
            "District"      => $this->_request->getParam('District'),
            "Wards"         => $this->_request->getParam('Wards'),
            "ResidenceAddress" => $this->_request->getParam('ResidenceAddress'),
            'StaffID'       => $currentStaff->StaffID,
            'PositionName'  => $currentStaff->Name,
            "XmlData"       => $this->_request->getParam('XmlData'),
            
        );
        $errors = array();
        if ($iRecordID) {
            // Sua
            $where = "RecordID ='$iRecordID'";
            $model->update($data,$where);
        } else {
            $data['RecordID'] = null;
            $data['CreateDate'] = date('Y/m/d H:i:s');
            $data['Status'] = 'TIEP_NHAN';
            $iRecordID = $model->insert($data);
        }
        
        echo Zend_Json::encode($errors);
        die;
        $this->_helper->getHelper('viewRenderer')->setNoRender();
    }
    /**
     * @see : Thuc hien viec xoa Mot hoac Nhieu van ban den
     */
    public function deleteAction() {
        $model = new module_models_Record;
        $sRecordIDList = $this->_request->getParam('RecordIDs');
        echo Zend_Json::encode($model->deleteRows($sRecordIDList));
        die();
    }

    /**
     * @see : Thuc hien viec in don
     */
    public function printrecordAction() {
        $objPublic = new Clound_Public();
        $iRecordID = $this->_request->getParam('RecordID','');
        //Gan gia tri mac dinh cho cac Input
        $model = new module_models_Record();
        $arrSingle = $model->selectRow($iRecordID);
        if($iRecordID){
            $recordtype = $arrSingle['RecordType'];
            $form = $arrSingle['Form'];
        }
        Zend_Loader::loadClass('Clound_phpdocx_createDocx');
        // $objPhpDocx = new Clound_phpdocx_createDocx();  
        $dirTemplate = './public/template/'.$recordtype.'/'.$form.'/DON.docx';
        $docx = new CreateDocxFromTemplate($dirTemplate);
        if($arrSingle) {
            $oCacheFile = new Clound_CacheFile();
            // Cap nhat trang thai
            $model->update(array('Status' => 'DA_IN'), "RecordID ='$iRecordID'");

            $xml = '<?xml version="1.0" encoding="UTF-8"?><root>'.$arrSingle['XmlData'].'</root>';
            $objXmlData = new Zend_Config_Xml($xml,'data_list');
            $data = $objXmlData->toArray();
            unset($arrSingle['XmlData']);
            unset($arrSingle['Temp']);
            $data = array_merge($data, $arrSingle);
            $data['DON_VI'] = 'huyện Lệ Thủy';
            $dmtl = 'TL_'.$form;
            $arrTL = $oCacheFile->getList($dmtl);
            $arrDocument = $objPublic->getNameByCodeList($arrTL, explode(',', $data['giaytolienquan']));
            $arrList = array('giaytolienquan' => $arrDocument);
            unset($data['giaytolienquan']);
            $arrCheckbox = array();
            // Xu ly checkbox
            switch ($form) {
                case 'HT01':
                        $denghi = $data['denghi'];
                        $arrDenghi = explode(',', $denghi);
                        foreach ($arrDenghi as $key => $value) {
                            $arrCheckbox['denghi'.$value] = ($value ? 1 : 0);
                        }
                        $arrNguongoc = explode(',', $data['nguongoctaolap']);
                        foreach ($arrNguongoc as $key => $value) {
                            $arrCheckbox['nguongoctaolap'.$value] = ($value ? 1 : 0);
                        }
                    break;
                
                default:
                    # code...
                    break;
            }
            // replace data
            $docx->tickCheckboxes ($arrCheckbox);
            $options = array('parseLineBreaks' =>true);
            $docx->replaceVariableByText($data, $options);
            //
            foreach ($arrList as $key => $items) {
                $docx->replaceListVariable($key, $items);
            }

        }

        $fileName = 'don_'.date('Ymd').'_'.time();
        $path = $_SERVER['SCRIPT_FILENAME'];
        $path = substr($path, 0, -9);
        $reportFile = str_replace("/", "\\", $path) . "io\\export\\" . $fileName;
        $docx->createDocx($reportFile);
        $reportFile = 'http://'.$_SERVER['HTTP_HOST'].$this->_request->getBaseUrl() ."/io/export/" . $fileName.".docx";
        echo $reportFile; die;        
    }
    
}?>
